package com.dudsbury.jtablestuff;

import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

import org.joda.time.DateTime;

import com.dudsbury.database.Booking;
import com.dudsbury.database.MyDateTime;

public class CustomTable extends JTable {

	private MyDateTime[] dateList;
	private MyDateTime currentDate;
	private String[] roomList;
	private DefaultTableModel tableModel;
	
	private ArrayList<Booking> bookingList;
	private Booking[][] rowData;
	
	public final int WEEKLY_VIEW = 0;
	 public final int MONTHLY_VIEW = 1;
	private int currentView = 0;

	
	public CustomTable(ArrayList<Booking> bookingList, String[] roomList) {

		this.bookingList = bookingList;
		this.roomList = roomList;

		setSize(400, 300);

		dateList = getWeekOfDates(new DateTime());
		currentView = 0;
		currentDate = new MyDateTime(new DateTime());
		setWeeklyView();
		//setRowData();
		TableCellRenderer renderer = new CustomTableCellRenderer();
		try {
			this.setDefaultRenderer(Class.forName("java.lang.Object"), renderer);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			System.exit(0);
		}
		
	}

	public void setRowData() {

		rowData = new Booking[roomList.length][dateList.length];
		
		

		for (int y = 0; y < dateList.length; y++) {
			for (int x = 0; x < roomList.length; x++) {

				for (Booking b : bookingList) {
					//System.out.println(dateList[y].getDateTime());
					if (b.getDate().getDayOfMonth() == dateList[y].getDateTime().getDayOfMonth() && b.getDate().getMonthOfYear() == dateList[y].getDateTime().getMonthOfYear() &&
							b.getDate().getYear() == dateList[y].getDateTime().getYear()) {
						rowData[x][y]=b;
						
						//System.out.println("Date Found" + b.getDate());
					}
				}

			}
		}
		
		tableModel = new DefaultTableModel(rowData, dateList);
		this.setModel(tableModel);
		this.repaint();
		System.out.printf("datelist: %d roomLIst: %d\n",rowData.length,rowData[0].length);
	}

	public void setWeeklyView() {
		
		dateList = getWeekOfDates(currentDate.getDateTime());
		
		
		
		setRowData();
		tableModel = new DefaultTableModel(rowData, dateList);
		
		this.setModel(tableModel);
		
		
		
		currentView = WEEKLY_VIEW;
		
	}

	public void setMonthlyView() {

		dateList = getMonthOfDates(currentDate.getDateTime());
		setRowData();
		tableModel = new DefaultTableModel(rowData, dateList);
		this.setModel(tableModel);
		repaint();
		currentView = MONTHLY_VIEW;
		
	}

	// Returns week of dates starting at the argument given
	public MyDateTime[] getWeekOfDates(DateTime d) {

		MyDateTime[] dateHold = new MyDateTime[7];
		//DateTime hold = new DateTime();
		for (int i = 0; i < 7; i++) {
			dateHold[i] = new MyDateTime(d.plusDays(i));

		}
		return dateHold;

	}

	// Returns month of dates starting at the argument given
	public MyDateTime[] getMonthOfDates(DateTime d) {

		//DateTime hold = new DateTime();
		MyDateTime[] dateHold = new MyDateTime[d.dayOfMonth()
				.getMaximumValue()];

		for (int i = 0; i < dateHold.length; i++) {
			dateHold[i] = new MyDateTime(d.plusDays(i));

		}
		return dateHold;

	}

	public int getCurrentView() {
		return currentView;
	}

	public void setCurrentView(int currentView) {
		this.currentView = currentView;
	}
	public MyDateTime getCurrentDate() {
		return currentDate;
	}

	public void setCurrentDate(MyDateTime currentDate) {
		this.currentDate = currentDate;
	}


}
